package com.lmabbe.tenant.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.tenant.objects.converter.SysParamsConverter;
import com.lmabbe.tenant.objects.dto.SysParamsDTO;
import com.lmabbe.tenant.objects.query.SysParamsQuery;
import com.lmabbe.tenant.objects.vo.SysParamsVO;
import com.lmabbe.tenant.service.SysParamsService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统参数表管理
 *
 * @author lmabbe
 */
@Api(tags = "系统参数表管理")
@RestController
@RequestMapping("sysParams")
@RequiredArgsConstructor
public class SysParamsController {

    private final SysParamsService sysParamsService;
    private final SysParamsConverter sysParamsConverter;

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<SysParamsVO>> list(SysParamsQuery query) {
        IPage page = sysParamsService.page(query.createPage(), query.createQuery());
        page.setRecords(sysParamsConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<SysParamsVO> getById(@PathVariable Long id) {
        return R.success(sysParamsConverter.entity2Vo(sysParamsService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody SysParamsDTO dto) {
        this.sysParamsService.save(sysParamsConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{paramId}")
    public R update(@RequestBody SysParamsDTO dto, @PathVariable Long paramId) {
        this.sysParamsService.updateById(sysParamsConverter.dto2Entity(dto, paramId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.sysParamsService.removeById(id);
        return R.success();
    }


    /**
     * 获取参数构建列表
     *
     * @return {@link R}
     * @date 2022/3/27 11:30 AM
     * @author liming
     */
    @GetMapping("getBuildList")
    public R getBuildList(String tenantId) {
        return R.success(sysParamsService.getBuildList(tenantId));
    }

}